<?php
require_once 'Class/BaseClass.php';
require_once 'Class/UserBase.php';
require_once 'Modules/BaseModule.php';
require_once 'Core/Session.php';

/**
 * 用户基础信息
 * */
class M_UserBase extends ModuleBase{
	
	/**
	 * 
	 * @param UserBase $UserBase
	 * @return NULL|UserBase
	 */
	public function single($UserBase=null){
		if(!isset($UserBase)||$UserBase===null||!isset($UserBase->Id)||empty($UserBase->Id)){
			return null;
		}
		$o=$this->_dishread('UserBase', $UserBase->Id);
		if($o===null){
			foreach($this->db->select('UserBase',array('Id'=>$UserBase->Id)) as $i){
				$o=$i;
			}
			if($o===null){
				return null;
			}else{
				$o=new UserBase($o);
				$this->_dishsave('UserBase', $o->Id, $o);
				return $o;
			}
		}else{
			return new UserBase($o);
		}
	}
	
	public function delete($UserBase=NULL){
		if($UserBase===null || !isset($UserBase->Id) || empty($UserBase->Id)){
			return 0;
		}
		$this->_dishclear('UserBase', $UserBase->Id);
		return $this->db->delete('UserBase',array('Id'=>$UserBase->Id));
	}
	
	/**
	 * 
	 * @param UserBase $UserBase
	 * @return UserBase
	 */
	public function insert($UserBase=null){
		if($UserBase===null||!isset($UserBase->Name)||empty($UserBase->Name))
			return null;
		$o=new UserBase($UserBase);
		$o->Id=$this->db->guid();
		$o->CreateDate=time();
		$o->ModifyDate=time();
		$o->Status=1;
		if($this->db->insert('UserBase',$o->__modetoarray())>0){
			$this->_dishsave('UserBase', $o->Id, $o);
		}
		else{
			$o->Id=null;
		}
		return $o;
	}
	
	/**
	 * 
	 * @param UserBase $UserBase
	 * @return int
	 */
	public function update($UserBase=null){
		if($UserBase===null){
			return 0;
		}
		$UserBase=new UserBase($UserBase);
		if($this->db->update('UserBase',$UserBase->__modetoarray(),array('Id'=>$UserBase->Id))>0){
			$this->_dishsave('UserBase', $UserBase->Id, $UserBase);
			return 1;
		}else{
			return 0;
		}
	}
	
	/**
	 * 
	 * @param $Id;$Name;$Email,$Mobile,$Status;$MinCreate;$MaxCreate;
	 */
	public function arraylist($search=null){
		
		$li=array();
		$cond=array();
		if(isset($search->Id) && !empty($search->Id)){
			$cond['Id']=$search->Id;
		}
		if(isset($search->Name) && !empty($search->Name)){
			$cond['Name']=$search->Name;
		}
		if(isset($search->Email) && !empty($search->Email)){
			$cond['Email']=$search->Email;
		}
		if(isset($search->Mobile) && !empty($search->Mobile)){
			$cond['Mobile']=$search->Mobile;
		}
		if(isset($search->Status) && is_numeric($search->Status)){
			$cond['Status']=$search->Status;
		}
		if(isset($search->MinCreate) && is_numeric($search->MinCreate)){
			$cond[]=array('CreateDate','>=',$search->MinCreate);
		}
		if(isset($search->MaxCreate) && is_numeric($search->MaxCreate)){
			$cond[]=array('CreateDate','<=',$search->MaxCreate);
		}
		$PageIndex=0;$PageSize=20;
		if($search!==null){
			if(is_numeric($search->PageIndex)){
				$PageIndex=$search->PageIndex;
			}
			if(is_numeric($search->PageSize)){
				$PageSize=$search->PageSize;
			}
		}
		foreach($this->db->select('UserBase',$cond,'[CreateDate] desc',array(intval($PageIndex),intval($PageSize))) as $i){
			$o=new UserBase();
			$o->Id=$i['Id'];
			$o->Name=$i['Name'];
			$o->UserSex=intval($i['UserSex'])>0?true:false;
			$o->BirthDay=intval($i['BirthDay']);
			$o->People=$i['People'];
			$o->Email=$i['Email'];
			$o->Mobile=$i['Mobile'];
			$o->Status=intval($i['Status']);
			$o->CreateDate=intval($i['CreateDate']);
			$o->ModifyDate=intval($i['ModifyDate']);
			$li[]=$o;
		}
		return $li;
	}
}
?>